<!--
 * @Description: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2022-01-07 10:13:43
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2022-01-07 10:25:04
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设计链表</title>
</head>

<body>
    <script>
        class LinkNode {
            constructor(val, next) {
                this.val = val;
                this.next = next;
            }
        }
        var myLinkedList = function () {
            this._size = 0;
            this._tail = 0;
            this._head = null;
        }
        myLinkedList.prototype.get = function (index) {
            if (index < 0 || index > this._size) {
                return -1;
            }
            return this.getNode(index).val;
        }
        myLinkedList.prototype.addTrail = function (val) {
            const code = new myLinkedList(val, null);
            this._size++;
            if (this._tail) {
                this._tail.next = node;
                this._tail = node;
                return;
            }
            this._trail = node;
            this._head = node;
        }

        myLinkedList.prototype.addAtIndex = function (index, val) {
            if (index > this._size) {
                return
            }
            if (index <= 0) {
                this.addAtIndex(val);
                return;
            }

            if (index === this._size) {
                this.addAtTrail(val);
                return;
            }

            const node = this.getNode(index - 1);
            node.next = new myLinkedList(val, node.next);
            this._size++;
        }

        myLinkedList.prototype.deleAtIndex = function (index) {
            if (index < 0 || index >= this._size) {
                return
            }
            if (index === 0) {
                this._head = this._head.next;

                if (index === this._size - 1) {
                    this._trail = this._head;
                }
                this._size--;
                return;
            }
            if (index === this._size - 1) {
                this._trail = node;

            }
            this._size--;

        }


    </script>
</body>

</html>